package com.ruoyi.web.service.Impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.pinyin.PinYinUtils;
import com.ruoyi.web.domain.mes.MesCustom;
import com.ruoyi.web.domain.mes.MesMaterialClass;
import com.ruoyi.web.domain.mes.MesPaper;
import com.ruoyi.web.domain.vo.MaterialBrowseVo;
import com.ruoyi.web.domain.vo.MaterialInVo;
import com.ruoyi.web.domain.vo.MaterialUseVo;
import com.ruoyi.web.domain.vo.MaterialVo;
import com.ruoyi.web.mapper.MesConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.mapper.MesMaterialMapper;
import com.ruoyi.web.domain.mes.MesMaterial;
import com.ruoyi.web.service.IMesMaterialService;

import javax.annotation.Resource;

/**
 * 物料管理Service业务层处理
 *
 * @author levicash
 * @date 2024-01-10
 */
@Service
public class MesMaterialServiceImpl implements IMesMaterialService {
    @Resource
    private MesMaterialMapper mesMaterialMapper;

    @Resource
    private MesConfigMapper configMapper;

    /**
     * 查询物料管理
     *
     * @param id 物料管理主键
     * @return 物料管理
     */
    @Override
    public MesMaterial selectMesMaterialById(Long id) {
        return mesMaterialMapper.selectMesMaterialById(id);
    }

    /**
     * 查询物料管理列表
     *
     * @param mesMaterial 物料管理
     * @return 物料管理
     */
    @Override
    public List<MesMaterial> selectMesMaterialList(MesMaterial mesMaterial) {
        return mesMaterialMapper.selectMesMaterialList(mesMaterial);
    }

    /**
     * 新增物料管理
     *
     * @param mesMaterial 物料管理
     * @return 结果
     */
    @Override
    public int insertMesMaterial(MesMaterial mesMaterial) {
        mesMaterial.setCreateBy(SecurityUtils.getUsername());
        mesMaterial.setCreateTime(DateUtils.getNowDate());
        // 生成物料编号
        String classCode = mesMaterial.getBelongClass();
        Map<String, Object> map = new HashMap<>();
        map.put("columnName", "code");
        map.put("tableName", "mes_material");
        int maxNum = configMapper.getMaxId(map);
        String code = PinYinUtils.getCodeNormal(maxNum, classCode);
        mesMaterial.setCode(code);
        return mesMaterialMapper.insertMesMaterial(mesMaterial);
    }

    /**
     * 修改物料管理
     *
     * @param mesMaterial 物料管理
     * @return 结果
     */
    @Override
    public int updateMesMaterial(MesMaterial mesMaterial) {
        mesMaterial.setUpdateBy(SecurityUtils.getUsername());
        mesMaterial.setUpdateTime(DateUtils.getNowDate());
        return mesMaterialMapper.updateMesMaterial(mesMaterial);
    }

    /**
     * 批量删除物料管理
     *
     * @param ids 需要删除的物料管理主键
     * @return 结果
     */
    @Override
    public int deleteMesMaterialByIds(Long[] ids) {
        return mesMaterialMapper.deleteMesMaterialByIds(ids);
    }

    /**
     * 删除物料管理信息
     *
     * @param id 物料管理主键
     * @return 结果
     */
    @Override
    public int deleteMesMaterialById(Long id) {
        return mesMaterialMapper.deleteMesMaterialById(id);
    }

    /**
     * 根据ids查询物料管理集合
     *
     * @param ids 需要查询的数据主键集合
     * @return 结果
     */
    @Override
    public List<MesMaterial> selectMesMaterialByIds(Long[] ids) {
        return mesMaterialMapper.selectMesMaterialByIds(ids);
    }

    /**
     * 查询物料数量
     *
     * @return 数量
     */
    @Override
    public int selectMesMaterialCount() {
        return mesMaterialMapper.selectMesMaterialCount();
    }

    /**
     * 修改物料信息状态
     *
     * @param mesMaterial 物料信息
     * @return 结果
     */
    @Override
    public int updateMesMaterialStatus(MesMaterial mesMaterial) {
        mesMaterial.setUpdateBy(SecurityUtils.getUsername());
        mesMaterial.setUpdateTime(DateUtils.getNowDate());
        return mesMaterialMapper.updateMesMaterial(mesMaterial);
    }


    /**
     * 查询物料一览
     *
     * @param map 查询参数
     * @return 物料一览集合
     */
    @Override
    public List<MaterialBrowseVo> selectMesMaterialBrowse(Map<String, Object> map) {
//        BigDecimal inventoryQuantity =  mesMaterialMapper.selectMesMaterialBrowseLastPD(map);
//        map.put("inventoryQuantity", inventoryQuantity);
        return mesMaterialMapper.selectMesMaterialBrowse(map);
    }

    @Override
    public List<MaterialVo> selectMesMaterialListDetail(Map<String, Object> map) {
        return mesMaterialMapper.selectMesMaterialListDetail(map);
    }

    @Override
    public List<MaterialVo> selectMaterialMachine(Map<String, Object> map) {
        return mesMaterialMapper.selectMaterialMachine(map);
    }

    @Override
    public List<MaterialUseVo> selectMaterialMachineDetail(Map<String, Object> map) {
        return mesMaterialMapper.selectMaterialMachineDetail(map);
    }

}
